<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\Account;

use AuctionCore\System\AuctionException;
use AuctionCore\Common\PhpMemcache;
use AuctionCore\Common\PhpRedis;
use AuctionCore\Activity\Lottory;


/**
 * 经纪人前台帐号
 * @author  爱民
 */
class AgentUser extends \AuctionCore\Account\FrontUser
{
    //不传密码，登录方式
    public static function loginByLoginKey($user_name, $login_key, $time)
    {

        //只限经纪人和车主可以登录
        $ou = new \AuctionCore\Account\OwnerUser($user_name);
        if ($ou->check_auth("owner_center") || $ou->check_auth("agent_center")) {
            return parent::loginByLoginKey($user_name, $login_key, $time);
        } else {
            AuctionException::make_throw('400003');
        }

    }

    /**
     * 创建经纪人用户
     * @param array $info [user_name=用户名，passwd=密码,nick_name=昵称,pwd_mobile=密保手机号,smspwd_mobile=短信手机号,in_code=邀请码]
     * @return int $user_id
     */
    public static function create($info, $is_md5 = false)
    {
        $in_code = $info['in_code'];
        unset($info['in_code']);

        //经纪人信息
        $in_aid = '';
        if ($in_code) {
            $in_des = \AuctionCore\Broker\Broker::get_detail_by_invite_code($in_code);
            if ($in_des)
                $in_aid = $in_des['aid'];
            else
                AuctionException::make_throw('200024');
        }
        $user_id = parent::create($info, $is_md5);
        if ($user_id) {
            $aid = \AuctionCore\Broker\Broker::create($user_id, $in_aid);
            //设置用户经纪人组
            $au = new \AuctionCore\Account\AgentUser($user_id);
            $au->set_group_by_title('agent_group');

            if ($in_code) { // 邀请注册 发红包
                $Rl = M('redpacket_limit');
                $cur_limit = $Rl->where([['day' => date("Y-m-d")]])->find();
                if (is_null($cur_limit)) {
                    $limit_num = M('setting')->where('name="RED_PACKET_LIMIT"')->getField('value');
                    $Rl->add(['limit_num' => $limit_num, 'day' => date('Y-m-d')]);
                }
                if ($cur_limit['have_use'] < $cur_limit['limit_num']) {
                    $redis = new PhpRedis(0);
                    $data = ['inviter' => $in_aid, 'invitee' => $aid, 'type' => 1, 'time' => date("Y-m-d H:i:s")];

                    $keyName = getRedisKeyByCode('REDIS_KEY','100006');
                    $redis->rPush($keyName, json_encode($data, JSON_UNESCAPED_UNICODE));
                    //$redis->rPush('invite_reg_list', json_encode($data, JSON_UNESCAPED_UNICODE));
                }
            }

            //检查活动 送奖券
            $Lottery = new \AuctionCore\Activity\Lottory();
            $ret = $Lottery->send_lottery_ticket($user_id,$in_code);
        }
        return $user_id;
    }
}
